<?php

namespace BookSpider\Provider;

use BookSpider\Http;
use BookSpider\SpiderBook;
/**
 * 顶点小说
 *
 * @domain      http://www.23wx.com/
 * @package		SpiderBook
 * @subpackage	Libraries
 * @author		Sanli
 * @time		2016/04/11 22:20
 */
class DingDian extends SpiderBook {
    
    public $host;
    public $config;
    
    public function __construct($config = array()) {
        $this->config = $config;
    }

    /**
     * 获取分类列表
     *
     * @return array|bool|void
     */
    public function category() {

        $_method = 'category';
        $url = $this->config['url'][$_method];

        $htm = Http::get($url, array('host' => $this->config['host']));
        $htm = $this->toUtf8($htm);

        return $this->match($htm, $this->config['rule'][$_method]);
    }

    /**
     * 获取分类的书籍列表
     *
     * @param int $category
     * @param int $page
     * @return array|bool|void
     */
    public function bookList($category, $page = 1) {

        $_method = 'bookList';
        $url = $this->config['url'][$_method];
        $url = $this->parseUrl($url, array($category, $page));
        $htm = Http::get($url, array('host' => $this->config['host']));
        $htm = $this->toUtf8($htm);

        return $this->match($htm, $this->config['rule'][$_method]);
    }

    /**
     * 获取章节目录
     *
     * @param $bookId
     * @return array|bool|void array('chapterId' => ..., 'chapterName' => ...)
     */
    public function chapter($bookId) {
        $_method = 'chapter';
        $url = $this->config['url'][$_method];
        $bookPrefix = substr($bookId, 0 ,2);
        $url = $this->parseUrl($url, array($bookPrefix, $bookId));

        $htm = Http::get($url, array('host' => $this->config['host']));
        $htm = $this->toUtf8($htm);

        return $this->match($htm, $this->config['rule'][$_method]);
    }

    /**
     * 获取章节内容
     *
     * @param $bookId
     * @param $chapterId
     * @return array|bool|void
     */
    public function content($bookId, $chapterId) {

        $_method = 'content';
        $url = $this->config['url'][$_method];
        $bookPrefix = substr($bookId, 0 ,2);
        $url = $this->parseUrl($url, array($bookPrefix, $bookId, $chapterId));

        $htm = Http::get($url, array('host' => $this->config['host']));
        $htm = $this->toUtf8($htm);

        return $this->match($htm, $this->config['rule'][$_method]);
    }

    /**
     * 获取小说基本信息
     *
     * @param $bookId
     */
    public function info($bookId) {

    }

    /**
     * 搜索小说
     *
     * @param $bookName
     * @return array|bool
     */
    public function search($bookName) {

        $_method = 'search';
        $url = $this->config['url'][$_method];
        $url = $this->parseUrl($url, array($bookName));

        $htm = Http::get($url, array('host' => $this->config['host']));
        $htm = $this->toUtf8($htm);

        return $this->match($htm, $this->config['rule'][$_method]);
    }

    /**
     * 生成小说
     * Todo 建立后台队列任务
     *
     * @param $bookId
     */
    public function make($bookId) {

        $book = array();

        // 获取小说信息
        $book['info'] = $this->info($bookId);

        // 获取小说章节
        $book['chapter'] = $this->chapter($bookId);

        // 获取小说章节内容
        foreach($book['chapter'] as $v) {
            $book['chapter']['content'] = $this->content($bookId, $v['chapterId']);
        }
    }

    /**
     * match
     *
     * @param $htm
     * @param $rule
     * @param bool|false $filter
     * @return array|bool
     */
    public function match($htm, $rule, $filter = false) {

        if(isset($rule['main']['start'])) {
            
            $content = $this->getMiddleText($htm, $rule['main']['start'], $rule['main']['end']);
        } else {
            $content = $htm;
        }
        
        $result = array();
        $ret = preg_match_all($rule['re'], $content, $result);
        if($ret) {
            foreach ($result as $k => $v) {
                if(!is_string($k)) {
                    unset($result[$k]);
                }                    
            }
        } else {
            return false;
        }
        return $result;
    }
}
